package com.yishun.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.service.impl.UserServiceImpl;
import com.yishun.springboot.util.ResultUtils;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserServiceImpl userService;

	/**
	 * 登录接口
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String username, String password) {

		try {
			userService.checkLogin(username, password);
			JSONArray jsa = userService.getRoleAndPage();
			return jsa.toJSONString();
		} catch (Exception e) {
			System.out.println("密码错误");
			return "login";
		}

	}

	/**
	 * 查询所有用户接口
	 * 
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("/getConditionInquire")
	public JSONObject getConditionInquire(@RequestBody JSONObject jsonObject) {
		return ResultUtils.jsonSuccess(userService.getConditionInquire(jsonObject));
	}

	/**
	 * 修改用户信息
	 * 
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("/upTbUser")
	public JSONObject upTbUser(@RequestBody JSONObject jsonObject) {
		return userService.upTbUser(jsonObject);
	}

	/**
	 * 删除用户 并且把归属于此账号的所有权限都删除
	 * 
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("/delUser")
	public String delUser(@RequestBody JSONObject jsonObject) {
		return userService.delUser(jsonObject).toJSONString();
	}

	@RequestMapping("/addUser")
	public String addUser(@RequestBody JSONObject jsonObject) {
		return userService.addUser(jsonObject).toJSONString();

	}

	@RequestMapping("/importUsers")
	public String importUsers(MultipartFile file) {
		return userService.importUsers(file).toJSONString();
	}
}
